package com.yg.controller;


import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.TechnologyCompetitionTemplate;
import com.yg.entity.singleEntity.TechnologyCompetition;
import com.yg.service.TechnologyCompetitionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 对应sheet13，此表是教师指导大学生参加科技竞赛表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@Slf4j
@Api(tags = "对应第13张表")

@RestController
@RequestMapping("/technologyCompetition")
@CrossOrigin //使其支持跨域资源共享
public class TechnologyCompetitionController {

    byte[] b = new byte[0];
    @Autowired
    private TechnologyCompetitionService service;
    /**
     * 上传到服务器的文件路径
     */
    private String uploadPath = "/Users/beibei/Desktop/英谷文件上传/sheet10_TeacherAchievementAward_Templates/";

    /**
     * 读取excel写入数据库
     * clr
     *
     * @param file 上传的文件
     * @return 统一返回R类
     */
    @ApiOperation(value = "上传文件")
    @PostMapping("/readExcel")
    public R readExcel(@ApiParam(value = "文件上传", required = true) MultipartFile file) {
        return service.readExcel(file);
    }

    /**
     * 导出excel
     * clr
     *
     * @return 统一返回R类
     */
    @ApiOperation(value = "下载包含所有数据的excel")
    @GetMapping("/writeExcel")
    public R writeExcel(@ApiParam(value = "excel下载") HttpServletResponse response) throws IOException {
        return service.writeExcel(response);
    }

    /**
     * 分页查询
     *
     * @param currentPage 当前页码
     * @param limit       每页需要呈现多少数据
     * @return
     */

    @ApiOperation(value = "分页查询")
    @GetMapping("/selectPage")
    public R selectAllByPage(@ApiParam(value = "当前页码") @RequestParam(value = "currentPage") int currentPage,
                             @ApiParam(value = "每页要展现的数据量") @RequestParam(value = "limit") int limit) {
        return service.selectPage(currentPage, limit);

    }

    /**
     * 从网页获取数据并插入到数据库
     *
     * @param entity 使用 TechnologyCompetition 封装从前端传入的表单数据
     * @return 返回影响的行数
     */
    @ApiOperation(value = "插入数据")
    @PostMapping("/insert")
    public R insert(@ApiParam(value = "用户要新增的表单数据") @RequestBody TechnologyCompetitionTemplate entity) {
        return service.insertOrUpdate(entity);

    }

    /**
     * 从网页获取数据并更新到数据库
     *
     * @param entity 使用 TechnologyCompetition 封装从前端传入的表单数据
     * @return 返回影响的行数
     */
    @ApiOperation(value = "更新数据")
    @PutMapping("/update")
    public R update(@ApiParam(value = "用户修改好的表单数据") @RequestBody TechnologyCompetitionTemplate entity) {
        return service.insertOrUpdate(entity);

    }

    /**
     * @param TechnologyCompetition 包含有主键id的实体类
     * @return 返回影响的行数
     */
    @ApiOperation(value = "根据id删除数据")
    @DeleteMapping("/deleteById")
    public R deleteById(@ApiParam(value = "传入的id") @RequestBody TechnologyCompetition TechnologyCompetition) {
        return service.deleteById(TechnologyCompetition);
    }


    /**
     * 模糊查询
     *
     * @param currentPage 当前页码
     * @param limit       每页需要呈现的数据条数
     * @param entity      使用entity接受模糊查询的条件参数
     * @return 统一返回R类
     */
    @ApiOperation(value = "模糊查询")
    @PostMapping("/selectByCondition")
    R selectByCondition(@ApiParam(value = "当前页码") @RequestParam(value = "currentPage") int currentPage,
                        @ApiParam(value = "每页要展现的数据量") @RequestParam(value = "limit") int limit,
                        @ApiParam(value = "模糊查询所需要的字段名") @RequestBody(required = false) TechnologyCompetition entity) {
        if (entity == null) {
            entity = new TechnologyCompetition();
        }
        return service.selectByCondition(currentPage, limit, entity);
    }

    /**
     * @param projectName 模糊查询所需要的属性名
     *                    //     * @param category    模糊查询所需要的属性名
     * @param response    response
     * @return 统一返回R类
     */
    @ApiOperation(value = "模糊查询后下载")
    @GetMapping("/writeByCondition")
    R writeByCondition(@ApiParam(value = "模糊查询所需要的属性名") @RequestParam(required = false) String projectName,
                       @ApiParam(value = "模糊查询所需要的属性名") @RequestParam(required = false) String category,
                       HttpServletResponse response) {

        TechnologyCompetition entity = new TechnologyCompetition();
        entity.setProjectName(projectName);
        entity.setCategory(category);
        return service.writeByCondition(entity, response);
    }
}

